unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, DBModule, IniFiles,
  SettingsForm, CountryForm, StateForm, CityForm, Vcl.ComCtrls, CustomersForm,
  Vcl.ExtCtrls, Vcl.StdCtrls, InvoiceForm, InvoiceListForm, EmployeeForm,
  SalaryForm, SystemOptionsForm, PaymentReceivedForm, Vcl.StdActns, Vcl.ActnList,
  ProductForm, ExpensesForm, SuppliersForm, InvoiceForm2, ReportsForm,
  PartTimeSettingForm, PartTimePaymentForm, Vcl.ToolWin, PaymentVoucherForm,
  IBDatabaseINI, System.Actions;

type
  TfmMain = class(TForm)
    MainMenu1: TMainMenu;
    Invoice1: TMenuItem;
    Master1: TMenuItem;
    Customer1: TMenuItem;
    N1: TMenuItem;
    Country1: TMenuItem;
    State1: TMenuItem;
    City1: TMenuItem;
    N2: TMenuItem;
    Exit1: TMenuItem;
    Options1: TMenuItem;
    StatusBar1: TStatusBar;
    NewInvoice1: TMenuItem;
    InvoiceList1: TMenuItem;
    Test1: TMenuItem;
    DBComfig1: TMenuItem;
    SystemConfig1: TMenuItem;
    EmployeeInfo: TMenuItem;
    SalaryPaySlip: TMenuItem;
    PaymentReceived1: TMenuItem;
    Expenses1: TMenuItem;
    ExpensesList1: TMenuItem;
    Report1: TMenuItem;
    PaymentReceived2: TMenuItem;
    ExpenseList1: TMenuItem;
    OverDuePayment1: TMenuItem;
    AccountStatement1: TMenuItem;
    CashFlow1: TMenuItem;
    LostProfit1: TMenuItem;
    Window1: TMenuItem;
    Cascade1: TMenuItem;
    ArrangeIcons1: TMenuItem;
    ActionList1: TActionList;
    WindowCascade1: TWindowCascade;
    WindowArrange1: TWindowArrange;
    MinimizeAll1: TMenuItem;
    WindowMinimizeAll1: TWindowMinimizeAll;
    TileHorizental1: TMenuItem;
    WindowTileHorizontal1: TWindowTileHorizontal;
    WindowTileVertical1: TWindowTileVertical;
    TileVertically1: TMenuItem;
    WindowClose1: TWindowClose;
    WindowTileVertical2: TWindowTileVertical;
    WindowClose2: TWindowClose;
    Close1: TMenuItem;
    N3: TMenuItem;
    WindowCascade2: TWindowCascade;
    WindowArrange2: TWindowArrange;
    WindowMinimizeAll2: TWindowMinimizeAll;
    N4: TMenuItem;
    Product1: TMenuItem;
    Supplier1: TMenuItem;
    JobsheetList1: TMenuItem;
    NewInvoicenoRTF1: TMenuItem;
    Memo1: TMemo;
    SQLMonitor1: TMenuItem;
    N5: TMenuItem;
    ReconnectDB1: TMenuItem;
    PartTimeSetting1: TMenuItem;
    N6: TMenuItem;
    PartTimePayment1: TMenuItem;
    ToolBar1: TToolBar;
    tbtnInvoiceList: TToolButton;
    tbtnNewInvoice: TToolButton;
    InvoiceListbyPhotographer1: TMenuItem;
    PaymentVoucher1: TMenuItem;
    Payment1: TMenuItem;
    IBDatabaseINI1: TIBDatabaseINI;
    procedure Exit1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Country1Click(Sender: TObject);
    procedure State1Click(Sender: TObject);
    procedure City1Click(Sender: TObject);
    procedure Customer1Click(Sender: TObject);
    procedure NewInvoice1Click(Sender: TObject);
    procedure InvoiceList1Click(Sender: TObject);
    procedure DBConfig1Click(Sender: TObject);
    procedure EmployeeInfoClick(Sender: TObject);
    procedure SalaryPaySlipClick(Sender: TObject);
    procedure SystemConfig1Click(Sender: TObject);
    procedure PaymentReceived1Click(Sender: TObject);
    procedure Product1Click(Sender: TObject);
    procedure ExpensesList1Click(Sender: TObject);
    procedure Supplier1Click(Sender: TObject);
    procedure NewInvoicenoRTF1Click(Sender: TObject);
    procedure ExpenseList1Click(Sender: TObject);
    procedure SQLMonitor1Click(Sender: TObject);
    procedure ReconnectDB1Click(Sender: TObject);
    procedure PartTimeSetting1Click(Sender: TObject);
    procedure PartTimePayment1Click(Sender: TObject);
    procedure tbtnNewInvoiceClick(Sender: TObject);
    procedure tbtnInvoiceListClick(Sender: TObject);
    procedure PaymentReceived2Click(Sender: TObject);
    procedure OverDuePayment1Click(Sender: TObject);
    procedure AccountStatement1Click(Sender: TObject);
    procedure LostProfit1Click(Sender: TObject);
    procedure InvoiceListbyPhotographer1Click(Sender: TObject);
    procedure PaymentVoucher1Click(Sender: TObject);
  private
    { Private declarations }
    procedure ConnectDB;

  public
    { Public declarations }
    IniConfig: TIniFile;
  end;

var
  fmMain: TfmMain;

implementation

{$R *.dfm}

procedure TfmMain.City1Click(Sender: TObject);
begin
  if fmCity = nil then
    fmCity := TfmCity.Create(Self);
  fmCity.Show;
end;

procedure TfmMain.ConnectDB;
var
  S: string;
begin
  S := IniConfig.ReadString('Config', 'DBLocation', '');
  if S <> '' then
  begin
    if IniConfig.ReadBool('Config', 'RemoteServer', False) then
      S := IniConfig.ReadString('Config', 'Hostname', '') + ':' + S;
    if DataModule1 = nil then
      DataModule1 := TDataModule1.Create(Self);
    with DataModule1 do
    begin
      IBTransaction1.Active := False;
      IBDatabase1.Connected := False;
      try
        IBDatabase1.DatabaseName := S;
        IBDatabase1.Connected := True;
        IBTransaction1.Active := True;
        StatusBar1.Panels[0].Text := 'DB Connected';
        fbCountry.Open;
        fbCustomers.Open;
        fbCity.Open;
        fbState.Open;
        fbInvoice.Open;
        fbInvoiceDetails.Open;
        fbPrintInvoiceDetails.Open;
//        qryConfig.Open;
        fbConfig.Open;
        qryInvoiceList.Open;
        qryPrintInvoice.Open;
        fbMarriageStatus.Open;
        fbJobPosition.Open;
        fbEmployee.Open;
        fbEmployee1.Open;
        fbEmployee2.Open;
        qryEmployeeSalary.Open;
        fbPaymentType.Open;
        fbMarriageStatus.Open;
        fbJobPosition.Open;
        fbProducts.Open;
        qryExpenses.Open;
        fbExpenseCategory.Open;
        fbExpenses.Open;
        fbSupplier.Open;
        fbPartTimeSetting.Open;
        StatusBar1.Panels[1].Text := 'DB Location:' + IBDatabase1.DatabaseName;
      except
        StatusBar1.Panels[0].Text := 'DB Disconnected';
        StatusBar1.Panels[1].Text := 'DB Location:';
      end;
    end;
  end else
    ShowMessage('DB Location is empty, go to Options->DB Config');
end;

procedure TfmMain.Country1Click(Sender: TObject);
begin
  if fmCountry = nil then
    fmCountry := TfmCountry.Create(Self);
  fmCountry.Show;
end;

procedure TfmMain.Customer1Click(Sender: TObject);
begin
  if fmCustomer = nil then
    fmCustomer := TfmCustomer.Create(Self);
  fmCustomer.Show;
end;

procedure TfmMain.Exit1Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TfmMain.ExpenseList1Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbExpensesList.Checked := True;
    rbExpensesListClick(nil);
    Show;
  end;
end;

procedure TfmMain.ExpensesList1Click(Sender: TObject);
begin
  if fmExpenses = nil then
    fmExpenses := TfmExpenses.Create(Self);
  fmExpenses.Show;
end;

procedure TfmMain.FormCreate(Sender: TObject);
begin
  IniConfig := TIniFile.Create('.\config.ini');
  SetLength(TrueBoolStrs, 1);
  SetLength(FalseBoolStrs, 1);
  TrueBoolStrs[0] := '1';
  FalseBoolStrs[0] := '0';
  ConnectDB;
  Caption := DataModule1.fbConfig.FieldByName('COMPANY_NAME').AsString;
end;

procedure TfmMain.InvoiceList1Click(Sender: TObject);
begin
  if fmInvoiceList = nil then
    fmInvoiceList := TfmInvoiceList.Create(Self);
  fmInvoiceList.DBGrid1.SetFocus;
  fmInvoiceList.Show;
end;

procedure TfmMain.InvoiceListbyPhotographer1Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbInvoiceByPhotographer.Checked := True;
    rbInvoiceByPhotographerClick(nil);
    Show;
  end;
end;

procedure TfmMain.LostProfit1Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbLostProfit.Checked := True;
    rbLostProfitClick(nil);
    Show;
  end;
end;

procedure TfmMain.EmployeeInfoClick(Sender: TObject);
begin
  if fmEmployeeInfo = nil then
    fmEmployeeInfo := TfmEmployeeInfo.Create(Self);
  fmEmployeeInfo.Show;
end;

procedure TfmMain.NewInvoice1Click(Sender: TObject);
begin
  if fmInvoice = nil then
    fmInvoice := TfmInvoice.Create(Self);
  fmInvoice.Show;
end;

procedure TfmMain.NewInvoicenoRTF1Click(Sender: TObject);
begin
  if fmInvoice2 = nil then
    fmInvoice2 := TfmInvoice2.Create(Self);
  fmInvoice2.Show;
end;

procedure TfmMain.OverDuePayment1Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbOverDuePayment.Checked := True;
    rbOverDuePaymentClick(nil);
    Show;
  end;
end;

procedure TfmMain.PartTimePayment1Click(Sender: TObject);
begin
  if fmPartTimePayment = nil then
    fmPartTimePayment := TfmPartTimePayment.Create(Self);
  fmPartTimePayment.Show;
end;

procedure TfmMain.PartTimeSetting1Click(Sender: TObject);
begin
  if fmPartTimeSetting = nil then
    fmPartTimeSetting := TfmPartTimeSetting.Create(Self);
  fmPartTimeSetting.Show;
end;

procedure TfmMain.AccountStatement1Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbAccountStatement.Checked := True;
    rbAccountStatementClick(nil);
    Show;
  end;
end;

procedure TfmMain.PaymentReceived1Click(Sender: TObject);
begin
  if fmPaymentReceived = nil then
    fmPaymentReceived := TfmPaymentReceived.Create(Self);
  fmPaymentReceived.DBGrid1.SetFocus;
  fmPaymentReceived.Show;
end;

procedure TfmMain.PaymentReceived2Click(Sender: TObject);
begin
  if fmReports = nil then
    fmReports := TfmReports.Create(Self);
  with fmReports do
  begin
    rbPaymentReceived.Checked := True;
    rbPaymentReceivedClick(nil);
    Show;
  end;
end;

procedure TfmMain.PaymentVoucher1Click(Sender: TObject);
begin
  if fmPaymentVoucher = nil then
    fmPaymentVoucher := TfmPaymentVoucher.Create(Self);
  fmPaymentVoucher.Show;
end;

procedure TfmMain.Product1Click(Sender: TObject);
begin
  if fmProducts = nil then
    fmProducts := TfmProducts.Create(Self);
  fmProducts.Show;
end;

procedure TfmMain.ReconnectDB1Click(Sender: TObject);
begin
  DataModule1.IBDatabase1.Connected := False;
  ConnectDB;
end;

procedure TfmMain.DBConfig1Click(Sender: TObject);
begin
  if fmSettings = nil then
    fmSettings := TfmSettings.Create(Self);
  with fmSettings do begin
    Visible := False;
    txtDBLocation.Text := IniConfig.ReadString('Config', 'DBLocation', '');
    txtHostname.Text := IniConfig.ReadString('Config', 'Hostname', '');
    txtPort.Text := IniConfig.ReadString('Config', 'Port', '');
    CheckBox1.Checked := IniConfig.ReadBool('Config', 'RemoteServer', False);
    rbPortriat.Checked := IniConfig.ReadInteger('Config', 'InvoiceListOrientation', 0) = 0;
    rbLandscape.Checked := IniConfig.ReadInteger('Config', 'InvoiceListOrientation', 0) = 1;
    txtExportPath.Text := IniConfig.ReadString('Config', 'Export Path', '');
    ShowModal;
    if txtDBLocation.Text <> '' then
      with IniConfig do
      begin
        WriteString('Config', 'DBLocation', txtDBLocation.Text);
        WriteString('Config', 'Hostname', txtHostname.Text);
        WriteString('Config', 'Port', txtPort.Text);
        WriteBool('Config', 'RemoteServer', CheckBox1.Checked);
        if rbPortriat.Checked then
          WriteInteger('Config', 'InvoiceListOrientation', 0)
        else;
          WriteInteger('Config', 'InvoiceListOrientation', 1);
        WriteString('Config', 'Export Path', txtExportPath.Text);
        UpdateFile;
      end;
  end;
  FreeAndNil(fmSettings);
  DataModule1.IBDatabase1.Connected := False;
  ConnectDB;
end;

procedure TfmMain.SalaryPaySlipClick(Sender: TObject);
begin
  if fmSalary = nil then
    fmSalary := TfmSalary.Create(Self);
  fmSalary.Show;
end;

procedure TfmMain.SQLMonitor1Click(Sender: TObject);
begin
  SQLMonitor1.Checked := not SQLMonitor1.Checked;
  Memo1.Visible := SQLMonitor1.Checked;
end;

procedure TfmMain.State1Click(Sender: TObject);
begin
  if fmState = nil then
    fmState := TfmState.Create(Self);
  fmState.Show;
end;

procedure TfmMain.Supplier1Click(Sender: TObject);
begin
  if fmSupplier = nil then
    fmSupplier := TfmSupplier.Create(Self);
  fmSupplier.Show;
end;

procedure TfmMain.SystemConfig1Click(Sender: TObject);
begin
  if fmSystemOptions = nil then
    fmSystemOptions := TfmSystemOptions.Create(Self);
  fmSystemOptions.Show;
end;

procedure TfmMain.tbtnInvoiceListClick(Sender: TObject);
begin
  InvoiceList1.Click;
end;

procedure TfmMain.tbtnNewInvoiceClick(Sender: TObject);
begin
  NewInvoicenoRTF1.Click;
end;

end.
